<?php
/**
 * FCJ Users database library
*
* @package FCJ\Database
*
*
* @author Felipe
*/
namespace FCJ\Database;

require_once ('php_Util_DB/php_Util_DB_PDO_lib.php');


/**
 * Namespace for PHP Util Database library
 *
*/
use php_Util\DB as ns_php_Util_DB;


use php_Util as ns_php_Util;

/**
 * Namespace for Database library
 *
 */
use FCJ\Database as ns_FCJ_Database;


/**
 * FCJ Users CONSTANTS
 *
 */
class FCJ_DB_Users_CONSTANTS {
	const USER_TYPE__EDUCATOR = 5001;
}


/**
 * FCJ Users table
 *
 */
class FCJ_DB_Users_TABLE {
	public $tableName = 'fcj_users';
	public $field_id = 'id';
	public $field_user_type = 'user_type';
	public $field_user_Name = 'user_Name';
}


/**
 * FCJ User data
 *
 */
class FCJ_DB_User_DATA {
	public $id = null;
	public $user_type = null;
	public $user_Name = null;
}


/**
 * FCJ User editor.
 * 
 * Provides CRUD facilities
 * 
 *
 */
class FCJ_DB_Users_EDITOR extends ns_php_Util_DB\php_Util_DB_EDITOR_PDO {

	/**
	 * @var FCJ_DB_Users_TABLE
	 */
	protected $table_Description = null;
	
	
	public function get_Description_Table() {
		return $this->table_Description;
	}
	
	
	public function __construct($table_Description = null) {
	
		if ($table_Description != null) {
			$this->table_Description = $table_Description;
		} else {
			$this->table_Description = ns_FCJ_Database\FCJ_DB_Users_FACTORY::get_NEW__FCJ_DB_Users_TABLE();
		}
	}
	
	
	/**
	 * Return array of insert fields for SQL insert
	 * @param FCJ_DB_User_DATA $object_DATA
	 */
	protected function get_Create_Fields($object_DATA) {
	
		$insertFields = array();

		$object_DATA->id !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_id => $object_DATA->id)) : null;
		$object_DATA->user_type !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_user_type => $object_DATA->user_type)) : null;
		$object_DATA->user_Name !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_user_Name => $object_DATA->user_Name)) : null;
		
		return $insertFields;
	}
	
	
	/**
	 * Return array of update fields for SQL insert
	 * @param FCJ_DB_User_DATA $object_DATA
	 */
	protected function get_Update_Fields($object_DATA) {
		
		$object_DATA_Update = clone $object_DATA;
		
		$object_DATA_Update->id = null;	// ID is a key. No update for this field. 
		$updateFields = $this->get_Create_Fields($object_DATA_Update);
		
		return $updateFields;
	}
	
	
	/**
	 * Read DATA from row array
	 * @param FCJ_DB_User_DATA $object_DATA
	 * @param array $row_Array
	 */
	protected function get_DATA_From_RowArray($object_DATA, $row_Array) {
		
		$object_DATA->id = $row_Array[$this->table_Description->field_id];
		$object_DATA->user_Name = $row_Array[$this->table_Description->field_user_type];
		$object_DATA->user_Name = $row_Array[$this->table_Description->field_user_Name];
		
	}
	
	
	/**
	 * CRUD method for CREATE object
	 * @param FCJ_DB_User_DATA $object_DATA
	 * @throws \Exception
	 * @return int affected rows
	 * 
	 */
	public function CRUD__Create($object_DATA) {
		
		// Check required parameters -----------------------------------------------------------------------|\/|---
		
		if (!is_a($object_DATA, "FCJ\Database\FCJ_DB_User_DATA")) {
			
			throw new \Exception("FCJ_DB_User_DATA is required.");
		}
		
		// -------------------------------------------------------------------------------------------------|/\|---
		
		
		// Create object -------------------------------------------------------------------------------------------------|\/|---
		$affected_Rows = $this->create_Object($this->table_Description->tableName, $this->get_Create_Fields($object_DATA));
		
		if ( $affected_Rows < 1 ) {
			throw new \Exception("Cannot create object.");
		}
		// ---------------------------------------------------------------------------------------------------------------|/\|---
		
		
		// Insert last insert ID in object ------------------------------------------|-|---
		$object_DATA->id = $this->db_Connection->_connection_Link->lastInsertId();
		
		
		return $affected_Rows;
	}
	
	
	/**
	 * CRUD method for READ object
	 * @param FCJ_DB_User_DATA $object_DATA
	 * @throws \Exception
	 *
	 */
	public function CRUD__Read($object_DATA) {
	
		// Check required parameters -----------------------------------------------------------------------|\/|---
		if (!is_a($object_DATA, 'FCJ\Database\FCJ_DB_User_DATA')) {
			throw new \Exception("FCJ_DB_User_DATA is required.");
		}
	
		// -------------------------------------------------------------------------------------------------|/\|---

		
		// Prepare conditions -----------------------------------------------------|\/|---
		$conditions_Array = array(
				$this->table_Description->field_id => $object_DATA->id
				);
		// ------------------------------------------------------------------------|/\|---
		
		
		
		// Read object ---------------------------------------------------------------------------------------------------|\/|---
		try {
			
			$result_DATA = $this->read_Object($this->table_Description->tableName, $conditions_Array, '*');
			
			if (sizeof($result_DATA) < 1) {
				throw new \Exception("Not found.");
			}
			
			$this->get_DATA_From_RowArray($object_DATA, $result_DATA[0]);
			
		} catch (Exception $e) {
			throw new \Exception("Error reading object. ". $e->getMessage());
		}
		// ---------------------------------------------------------------------------------------------------------------|/\|---
		
	}
	

	/**
	 * CRUD method for UPDATE object
	 * @param FCJ_DB_User_DATA $object_DATA
	 * @throws \Exception
	 * @return int affected rows
	 *
	 */
	public function CRUD__Update($object_DATA) {
	
		// Check required parameters -----------------------------------------------------------------------|\/|---
		if (!is_a($object_DATA, 'FCJ\Database\FCJ_DB_User_DATA')) {
			throw new \Exception("FCJ_DB_User_DATA is required.");
		}
		// -------------------------------------------------------------------------------------------------|/\|---
		

		// Prepare conditions -----------------------------------------------------|\/|---
		$conditions_Array = array(
				$this->table_Description->field_id => $object_DATA->id
		);
		// ------------------------------------------------------------------------|/\|---
		
		
		// Update object ---------------------------------------------------------------------------------------------------------------------------|\/|---
		$affected_Rows = $this->update_Object($this->table_Description->tableName, $this->get_Update_Fields($object_DATA), $conditions_Array);
	
		if ( $affected_Rows < 1 ) {
			throw new \Exception("Cannot update object.");
		}
		// -----------------------------------------------------------------------------------------------------------------------------------------|/\|---
		
	
		return $affected_Rows;
	}

	
	/**
	 * CRUD method for DELETE object
	 * @param FCJ_DB_User_DATA $object_DATA
	 * @throws \Exception
	 * @return int affected rows
	 *
	 */
	public function CRUD__Delete($object_DATA) {
	
		// Check required parameters -----------------------------------------------------------------------|\/|---
		if (!is_a($object_DATA, 'FCJ\Database\FCJ_DB_User_DATA')) {
			throw new \Exception("FCJ_DB_User_DATA is required.");
		}
		// -------------------------------------------------------------------------------------------------|/\|---
	
	
		// Prepare conditions -----------------------------------------------------|\/|---
		$conditions_Array = array(
				$this->table_Description->field_id => $object_DATA->id
		);
		// ------------------------------------------------------------------------|/\|---
	
	
		// Delete object ---------------------------------------------------------------------------------------------------------------------------|\/|---
		$affected_Rows = $this->delete_Object($this->table_Description->tableName, $conditions_Array);
	
		if ( $affected_Rows < 1 ) {
			throw new \Exception("Cannot delete object.");
		}
		// -----------------------------------------------------------------------------------------------------------------------------------------|/\|---
	
	
		return $affected_Rows;
	}
	
}


/**
 * 
 * FCJ Users Database Factory
 * 
 * Provides facilities for create Users related objects
 * 
 * @author Felipe
 *
 */
class FCJ_DB_Users_FACTORY {


	public static function get_NEW__FCJ_DB_Users_TABLE() {
		return new FCJ_DB_Users_TABLE();
	}

	public static function get_NEW__FCJ_DB_User_DATA() {
		return new FCJ_DB_User_DATA();
	}

	
	public static function get_NEW__FCJ_DB_Users_EDITOR() {
		return new FCJ_DB_Users_EDITOR();
	}
	
	


	/**
	 * Users search facility by conditions
	 * @param array|string $conditions
	 * @param php_Util_DB_CONNECTION $connection
	 * @return array
	 *
	 * @throws \Exception
	 */
	public static function get__Users_by_Conditions($conditions, $connection) {
	
		$fcj_DB_Users_EDITOR = self::get_NEW__FCJ_DB_Users_EDITOR();
		$fcj_DB_Users_TABLE = $fcj_DB_Users_EDITOR->get_Description_Table();
	
	
		// Set connection -------------------------------------------------------|\/|---
		try {
			$fcj_DB_Users_EDITOR->connect_To_DB($connection);
		} catch (\Exception $e) {
			throw new \Exception("Error in connection. ". $e->getMessage());
		}
		// ----------------------------------------------------------------------|/\|---
	
	
		// Get result array -----------------------------------------------------------------|\/|---
		$result_Array = array();
	
		try {
			$result_Array = $fcj_DB_Users_EDITOR->read_Object($fcj_DB_Users_TABLE->tableName, $conditions, '*');
		} catch (\Exception $e) {
			throw new \Exception("Error reading object. ". $e->getMessage());
		}
		// ----------------------------------------------------------------------------------|/\|---
	
	
		// Get users data from result array ------------------------------------------------------------------------------|\/|---
		$array_Groups_DATA = array();
	
		for ($index_Result = 0; $index_Result < sizeof($result_Array); $index_Result++) {
			$fcj_DB_Users_DATA = ns_FCJ_Database\FCJ_DB_Users_FACTORY::get_NEW__FCJ_DB_Group_DATA();
			$fcj_DB_Users_EDITOR->get_DATA_From_RowArray($fcj_DB_Users_DATA, $array_Groups_DATA[$index_Result]);
			array_push($array_Groups_DATA, $fcj_DB_Users_DATA);
		}
		// ----------------------------------------------------------------------------------------------------------------|/\|---
	
	
		return $array_Groups_DATA;
	
	}
	
}


